package com.naizhuai.ui.task.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.entity.Task.BehaviorRecommendation;
import com.naizhuai.ui.entity.mainbusiness.ActionBaseBean;
import com.naizhuai.ui.mianbusiness.action.ActionBrightSpotActivity;
import com.naizhuai.util.ViewUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.subjects.PublishSubject;

/**
 * @author：lichenxi
 * @date 2018/5/16 17
 * email：525603977@qq.com
 * Fighting
 */
public class ImageSelectorAdapter extends RecyclerView.Adapter<ImageSelectorAdapter.ViewHolder> {

    private List<BehaviorRecommendation> mRecommendSchemes;
    private int selected = -1;
     public ImageSelectorAdapter() {
     }
    public PublishSubject<BehaviorRecommendation> SelectorSubject=PublishSubject.create();

    public PublishSubject<BehaviorRecommendation> getSelector() {
        return SelectorSubject;
    }

    public void setSelectSchemes(List<BehaviorRecommendation> recommendSchemes) {
        mRecommendSchemes = recommendSchemes;
          if (mRecommendSchemes.size()==1){
              selected=0;
              SelectorSubject.onNext(mRecommendSchemes.get(0));
          }
        notifyDataSetChanged();
    }

    public void setRecommendSchemes(List<BehaviorRecommendation> recommendSchemes) {
          mRecommendSchemes = recommendSchemes;
//          if (mRecommendSchemes.size()==1){
//              selected=0;
//              SelectorSubject.onNext(mRecommendSchemes.get(0));
//          }
          notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.imageselect_action, parent, false);
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BehaviorRecommendation scheme =  mRecommendSchemes.get(position);
        ViewUtils.showImg(scheme.schemeImage,holder.imageView);
        holder.actionName.setText(scheme.schemeId);
        if(selected == position){
            holder.cbImageCheck.setSelected(true);
        } else {
            holder.cbImageCheck.setSelected(false);
        }
        holder.cbImageCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.cbImageCheck.isSelected()){
                    selected=-1;
                }else {
                    selected=holder.getAdapterPosition();
                }
                SelectorSubject.onNext(scheme);
                notifyDataSetChanged();
            }
        });
       ViewUtils.setTouchDelegate(holder.cbImageCheck,80);

       holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(holder.itemView.getContext(),ActionBrightSpotActivity.class);
                intent.putExtra(ActionBaseBean.ACTION_ID_KEY,scheme.questionId);
                holder.itemView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mRecommendSchemes != null ? mRecommendSchemes.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageView)
        ImageView imageView;
        @BindView(R.id.actionName)
        TextView actionName;
        @BindView(R.id.cb_image_check)
        AppCompatImageView cbImageCheck;
        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
